package com.doge.walknovel.business.home.profile

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.doge.wnpro.BuildConfig
import com.doge.wnpro.R
import com.doge.walknovel.api.model.UserInfo
import com.doge.walknovel.business.WebActivity
import com.doge.walknovel.business.home.profile.push.PushManagementActivity
import com.doge.walknovel.business.home.profile.topup.TopUpActivity
import com.doge.walknovel.business.home.profile.topup.TopUpRefer
import com.doge.walknovel.business.home.profile.utils.CacheUtils
import com.doge.walknovel.business.home.profile.viewed.ViewedHistoryActivity
import com.doge.walknovel.business.home.profile.wallet.WalletHistoryActivity
import com.doge.walknovel.business.login.LoginViewModel
import com.doge.walknovel.core.BaseFragment
import com.doge.walknovel.core.account.AccountHelper
import com.doge.walknovel.core.account.AccountHelper.selfInfo
import com.doge.walknovel.core.base.isRefreshing
import com.doge.walknovel.urlPolicy
import com.doge.wnpro.databinding.FragmentProfileBinding
import com.doge.walknovel.utils.*
import com.doge.walknovel.widgets.AlertDialog

class ProfileFragment : BaseFragment() {
    private var innerBinding: FragmentProfileBinding? = null
    private val binding get() = innerBinding!!
    private val logoutViews by lazy { arrayOf(binding.logoutText,binding.logoutImage, binding.logout) }
    private val viewModel: LoginViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initialViews()
        binding.version.text = BuildConfig.VERSION_NAME
        binding.cacheValue.text = CacheUtils.calcTotalSize(appContext)

        selfInfo.observe(viewLifecycleOwner) { user ->
            binding.avatar.load(
                user?.profilePicture,
                R.mipmap.avatar_default,
                CircleCrop,
                width = 78.dp,
                height = 78.dp
            )
            binding.nickname.text = user?.nickName
            binding.desc.text = user?.memberDescribe
            binding.walletValue.isVisible = user != UserInfo.EMPTY
            binding.walletValue.text =
                String.format(getString(R.string.xxx_coins), (user?.coinNum ?: 0).toString())
            logoutViews.forEach { it.isVisible = user?.isLogin == true }
            binding.cacheValue.isVisible = user != UserInfo.EMPTY
            binding.cacheValue.text = CacheUtils.calcTotalSize(appContext)
        }

        CacheUtils.loading.observe(viewLifecycleOwner) {
            binding.cacheLoading.isVisible = it.isRefreshing()
            if (!it.isRefreshing()) binding.cacheValue.text = CacheUtils.calcTotalSize(appContext)
        }
    }

    private fun initialViews() {
        binding.avatarCard.setOnClickCallback {  AccountHelper.requiredLogin() }
        binding.topUp.setOnClickCallback { TopUpActivity.start(it.context, TopUpRefer.profile) }
        binding.walletHistory.setOnClickCallback { WalletHistoryActivity.start(it.context) }
        binding.viewed.setOnClickCallback { ViewedHistoryActivity.start(it.context) }
        binding.push.setOnClickCallback { PushManagementActivity.start(it.context) }
        binding.cache.setOnClickCallback { CacheUtils.clear(appContext) }
        binding.policy.setOnClickCallback { WebActivity.start(it.context, urlPolicy) }
        binding.logout.setOnClickCallback { showLogoutDialog() }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.topUp.setOnClickListener(null)
        binding.walletHistory.setOnClickListener(null)
        binding.push.setOnClickListener(null)
        binding.viewed.setOnClickListener(null)
        binding.logout.setOnClickListener(null)
        binding.cache.setOnClickListener(null)
        binding.policy.setOnClickListener(null)
        innerBinding = null
    }

    private fun showLogoutDialog() {
        val context = context ?: return
        AlertDialog.Builder(context)
            .setMessage(getString(R.string.sure_you_want_to_log_out))
            .setNegativeButton(getString(R.string.cancel1)) { _, _ -> }
            .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                viewModel.logout()
            }
            .create().show()
    }
}